import router from '@/router'
import store from '@/store'
// 权限拦截
// 全局的路由守卫
router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  const nextTo = ['/login', '/404'] // 白名单，没有token没有登录之前只能进入的页面
  if (token) {
    // 登录
    // console.log(to)
    if (to.path === '/login') return next('/')
    // 获取用户信息
    await store.dispatch('user/getUserInfo')
    next()
  } else {
    if (nextTo.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  next()
})
